<?php

namespace App\Admin\Controllers\Order;

use App\Http\Controllers\Controller;
use App\Models\Order\Model\Cash;
use App\Models\Setting\Model\UnitType;
use App\Models\Setting\Repository\UnitRepository;
use App\Models\Setting\Repository\UnitTypeRepository;
use App\Models\User\Model\User;
use App\Models\User\Repository\UserRepository;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class CashController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('流水')
            ->description('详情')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('流水')
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('详情')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增')
            ->description('详情')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Cash());
        $userRepository = new UserRepository();

        $grid->filter(function($filter) use($userRepository){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('title', '标题');
            $filter->like('user_id', '用户')->select($userRepository->getUsers());

            $filter->between('shopped_at','消费时间')->datetime();
        });

        $grid->id('ID')->sortable();
        $grid->shopped_at('消费时间');
        $grid->user()->name('用户名');
        $grid->totals('总价');
        $grid->title('标题');
        //$grid->updated_at('Updated at');
        $grid->actions(function ($actions) {
            //$actions->disableDelete();
            //$actions->disableEdit();
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Cash::findOrFail($id));

        $show->id('ID');
        $show->title('标题');
        $show->content('内容');
//        $show->status('状态')->with(function ($value){
//            return 12121;
//        });
        $show->type('ID');
        $show->pay_way('ID');
        $show->user_id('ID');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Cash());
        $unitRepository = new UnitRepository();
        $unitTypeRepository = new UnitTypeRepository();
        $userRepository = new UserRepository();

        $form->display('id', 'ID');
        $form->text('title','标题');
        $form->textarea('content','内容');
        $form->datetime('shopped_at','消费时间');
        $form->select('status','状态')->options($unitTypeRepository->statusArray())->default(UnitType::UP);
        $form->select('type','类型')->options($unitRepository->getShopType());
        $form->select('pay_way','支付方式')->options($unitRepository->getPayWayType());
        $form->select('user_id','用户')->options($userRepository->getUsers());
//        $form->display('created_at', '创建时间');
//        $form->display('updated_at', '更新时间');

        $form->hasMany('products','商品',function (Form\NestedForm $form)use($unitRepository){
            $form->decimal('total','总价');
            $form->decimal('number','数量');
            $form->text('name','名称');
            $form->select('unit','单位')->options($unitRepository->getUnitType());
        });

        return $form;
    }
}
